package com.dongge.service;

import com.dongge.dao.CURDDaoBase;
import com.dongge.entity.Right;
import com.dongge.entity.Role;
import com.dongge.entity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;

/**
 * Created by Administrator on 2015/11/3.
 */
@Service
public class UserService extends CURDDaoBase<User> {
    @Resource
    private RoleService roleService;

    /**
     * 获取当前角色所有权限的id数组
     * @param id
     * @return
     */
    public long[] getUserRoleIdsById(Long id) {
        if(id == null)
            return null;
        User user = get(id);
        Set<Role> roles = user.getRoles();
        int i = 0;
        long[] roleIds = new long[roles.size()];
        for (Role role : roles) {
            roleIds[i] = role.getId();
            i = i + 1;
        }
        return roleIds;
    }

    /**
     * 更新角色的权限
     * @param id
     * @param roleIds
     */
    public void updateUserRoleById(Long id,long[] roleIds){
        if(id == null|| roleIds == null)
            return;
        User user = get(id);
        for(long roleId:roleIds){
            Role role = roleService.get(roleId);
            user.getRoles().add(role);
        }
    }
}
